package com.ruoyi.project.equipment.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.equipment.domain.UserEquipment;
import com.ruoyi.project.equipment.mapper.UserEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.equipment.mapper.EquipmentMapper;
import com.ruoyi.project.equipment.domain.Equipment;
import com.ruoyi.project.equipment.service.EquipmentService;

/**
 * 设备信息Service业务层处理
 *
 * @author ruoyi
 * @date 2020-07-03
 */
@Service
public class EquipmentServiceImpl implements EquipmentService
{
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private UserEquipmentMapper userEquipmentMapper;

    /**
     * 查询设备信息
     *
     * @param equipmentId 设备信息ID
     * @return 设备信息
     */
    @Override
    public Equipment selectEquipmentById(Long equipmentId)
    {
        return equipmentMapper.selectEquipmentById(equipmentId);
    }
    /**
     * 查询设备信息列表
     *
     * @param equipment 设备信息
     * @return 设备信息
     */
    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    /**
     * 新增设备信息
     *
     * @param equipment 设备信息
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        // 新增设备信息
        int rows = equipmentMapper.insertEquipment(equipment);
        // 新增设备用户关联
        insertUserEquipment(equipment);
        return rows;
    }

    /**
     * 修改设备信息
     *
     * @param equipment 设备信息
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        Long equipmentId = equipment.getId();
        // 删除用户与设备关联
        userEquipmentMapper.deleteUserEquipmentById(equipmentId);
        // 新增设备用户关联
        insertUserEquipment(equipment);
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 新增设备用户信息
     *
     * @param equipment 用户对象
     */
    public void insertUserEquipment(Equipment equipment)
    {
        Long[] userIds = equipment.getUserIds();
        if (StringUtils.isNotNull(userIds))
        {
            // 新增用户与角色管理
            List<UserEquipment> list = new ArrayList<UserEquipment>();
            for (Long userId : userIds)
            {
                UserEquipment ue = new UserEquipment();
                ue.setId(equipment.getId());
                ue.setUserId(userId);
                list.add(ue);
            }
            if (list.size() > 0)
            {
                userEquipmentMapper.batchUserEquipment(list);
            }
        }
    }
    /**
     * 批量删除设备信息
     *
     * @param equipmentIds 需要删除的设备信息ID
     * @return 结果
     */
    @Override
    public int deleteEquipmentByIds(Long[] equipmentIds)
    {
        return equipmentMapper.deleteEquipmentByIds(equipmentIds);
    }

    /**
     * 删除设备信息信息
     *
     * @param equipmentId 设备信息ID
     * @return 结果
     */
    @Override
    public int deleteEquipmentById(Long equipmentId)
    {
        return equipmentMapper.deleteEquipmentById(equipmentId);
    }

    @Override
    public int updateEquipmentStatus(Equipment equipment){
        return equipmentMapper.updateEquipment(equipment);
    }
}